﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;

namespace QA_System
{
    public class NPAnaphoraDisCover:IDisCoverInterface
    {
        #region IDisCoverInterface Members

        public bool IsOccup(SentenceStruct sen)
        {
            try
            {
                bool result = false;
                for (int i = 0; i < sen.SentenceLength; i++)
                {
                    if (((sen[i].Pos == "P") && (sen[i].Ower != -1)))
                    {
                        if(!(sen[i].Word.Equals("đâu") || sen[i].Word.Equals("bao nhiêu")|| sen[i].Word.Equals("ai") ||sen[i].Word.ToLower().Equals("tôi")))
                        result = true;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
              
      // mac dinh la intra - anaphora
        public string TypeOccup(SentenceStruct refsen, SentenceStruct sen)
        {
            try
            {
                string result = "";
                int[] ProNounIndex = sen.GetProNounsPosition();
                int[] ReferenceIndex = sen.GetNpPosition();
                if ((ReferenceIndex.Length > 0) && (ProNounIndex.Length > 0)) if (ProNounIndex[0] > (ReferenceIndex[0] + 1)) result += "Interable";
                    else
                    {
                        ReferenceIndex = refsen.GetNpPosition();
                        int index = 0;
                        int GenderXX = 0;
                        int AllHuman = 0;
                        while (index < ReferenceIndex.Length)
                        {
                            if (refsen[ReferenceIndex[index]].WordSubMean.Contains("People"))
                            {
                                AllHuman++;
                                if (refsen[ReferenceIndex[index]].WordSubMean.Contains("XX")) GenderXX++;
                            }
                            index++;
                        }
                        if ((GenderXX > 2) || ((AllHuman - GenderXX) >= 2)) result += "Complex";
                    }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        internal bool IsOccup(sentence_tagger resultSen)
        {
            throw new NotImplementedException();
        }
    }

    public class VPAnaphoraDisCover : IDisCoverInterface
    {
        #region IDisCoverInterface Members

        public bool IsOccup(SentenceStruct sen)
        {
            try
            {
                bool result = false;
                int[] intAllPreverb = sen.GetAllPreVerbPosition2();
                if (intAllPreverb.Length > 0)
                {
                    int i = 0;
                    for (i = 0; i < intAllPreverb.Length; i++)
                    {
                        if (!sen[intAllPreverb[i] + 1].Pos.Equals("V") && (!sen[intAllPreverb[i] + 1].Pos.Equals("A")) && (!sen[intAllPreverb[i] + 1].Pos.Equals("M")) && (!sen[intAllPreverb[i] + 1].Pos.Equals("Nc"))) result = true;
                        else
                        {
                            if (sen[intAllPreverb[i] + 1].Pos.Equals("V") && ((sen[intAllPreverb[i] + 1].Word.Equals("thế")) || (sen[intAllPreverb[i] + 1].Word.Equals("vậy")) || (sen[intAllPreverb[i] + 2].Word.Equals("như vậy")) || (sen[intAllPreverb[i] + 2].Word.Equals("như thế"))))
                            {
                                result = true;
                            }
                        }
                    }
                }
                
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string TypeOccup(SentenceStruct refsen, SentenceStruct sen)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class SPAnaphoraDisCover : IDisCoverInterface
    {
        #region IDisCoverInterface Members
        static string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
        public bool IsOccup(SentenceStruct sen)
        {
            try
            {
                bool result = false;
                int[] inAllVerb = sen.GetAllVerbPosition();
                int[] inAllNoun = sen.GetNounsPosition();

                SAXMLConnection SAna = new SAXMLConnection();
                int j = sen.SentenceLength;
                for (int i = 0; i <= j - 2; i++)
                {
                    if (SAna.IsSA(sen[i].Word.ToLower(), xmlDatabasePath+"SAnaphoraXMLdatabase.xml"))
                    {
                    //if (sen[1].Word.ToLower().Equals("là")) 
                        if (sen[i].Word.Equals("đó")|| sen[i].Word.Equals("đấy"))
                        {
                            if (sen[i - 1].Word.Equals("điều"))
                                result = true;
                            else
                                result = false;
                        }
                        else
                            result = true;

                    }
                }
                
                //else
                //{
                    if (inAllVerb.Length == 1)
                    {
                        if ((sen[inAllVerb[0]].Word.Equals("nghĩ")) || (sen[inAllVerb[0]].Word.Equals("đoán")) || (sen[inAllVerb[0]].Word.Equals("thấy")))
                        {
                            if ((sen[sen.SentenceLength - 2].Word.Equals("thế")) || (sen[sen.SentenceLength - 2].Word.Equals("vậy"))) result = true;
                        }
                    //}
                }

                /* code goc
                if ((sen[0].Word.ToLower().Equals("đó")) || (sen[0].Word.ToLower().Equals("đây")) || (sen[0].Word.ToLower().Equals("đấy")))
                {
                    if (sen[1].Word.ToLower().Equals("là")) result = true;
                }
                else
                {
                    if (inAllVerb.Length == 1)
                    {
                        if ((sen[inAllVerb[0]].Word.Equals("nghĩ")) || (sen[inAllVerb[0]].Word.Equals("đoán")) || (sen[inAllVerb[0]].Word.Equals("thấy")))
                        {
                            if ((sen[sen.SentenceLength - 2].Word.Equals("thế")) || (sen[sen.SentenceLength - 2].Word.Equals("vậy"))) result = true;
                        }
                    }
                }
                 */
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string TypeOccup(SentenceStruct refsen, SentenceStruct sen)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class OneAnaphoraDisCover : IDisCoverInterface
    {
        #region IDisCoverInterface Members
        static string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
        public bool IsOccup(SentenceStruct sen)
        {
            try
            {
                bool result = false;
                int[] intAllNc = sen.GetAllNcPosition();
                if (intAllNc.Length > 0)
                {
                    int i = 0;
                    for (i = 0; i < intAllNc.Length; i++)
                    {
                        if ((!sen[intAllNc[i] + 1].Pos.Contains("N"))&&(!sen[intAllNc[i]+1].Pos.Contains("P"))) result = true;
                    }
                }

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsOccupNew( ref SentenceStruct sen)
        {
            try
            {
                bool result = false;
                OneAnaXMLConnection OneAna = new OneAnaXMLConnection();
                int j = sen.SentenceLength;
                for (int i = 0; i <j; i++)
                {
                    if (OneAna.IsOneAnaphora(sen[i].Word.ToLower(), xmlDatabasePath + "OneAnaphoraXMLdatabase.xml"))
                    {
                        //if (sen[1].Word.ToLower().Equals("là")) 
                        if ((!sen[i + 1].Pos.Contains("N")) && (!sen[i+1].Pos.Contains("P"))) result = true;
                        sen[i].Pos = "Nc";

                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string TypeOccup(SentenceStruct refsen, SentenceStruct sen)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
